package com.zone.core.binding

import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.widget.LinearLayout
import androidx.annotation.LayoutRes
import androidx.databinding.BindingAdapter
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.viewpager2.widget.ViewPager2
import com.zone.core.CoreApplication
import com.zone.core.base.BaseActivity
import com.zone.core.base.BaseBindingAdapter
import com.zone.core.base.adapter.DefaultBindingAdapter
import com.zone.core.base.adapter.SimpleFragmentAdapter

/**
 *Author: last
 *Time: 2024-09-04
 *Description: ViewPager2
 */

@BindingAdapter("pageFragments")
fun setData(viewPager: ViewPager2, pageFragments: List<Fragment>) {
    val activity = CoreApplication.currentActivity() as BaseActivity
    val adapter = SimpleFragmentAdapter<Fragment>(
            viewPager,
            activity.supportFragmentManager,
            activity.lifecycle
        )
    adapter.fragments = pageFragments
    viewPager.adapter = adapter
}

@BindingAdapter("pageFragments", "changeListener")
fun setData(
    viewPager: ViewPager2,
    pageFragments: List<Fragment>,
    changeListener: SimpleFragmentAdapter.OnItemChangeListener<Fragment>
) {
    val activity = CoreApplication.currentActivity() as BaseActivity
    val adapter = SimpleFragmentAdapter<Fragment>(
        viewPager,
        activity.supportFragmentManager,
        activity.lifecycle
    )

    adapter.itemChangeListener = changeListener
    adapter.fragments = pageFragments
    viewPager.adapter = adapter
}

@BindingAdapter("recyclerData", "itemLayout", "itemEventHandler")
fun setData(
    recyclerView: ViewPager2,
    data: List<Any>?,
    @LayoutRes itemLayout: Int,
    itemEventHandler: Any
) {
    setData(recyclerView, data, itemLayout, null, null, itemEventHandler)
}

@BindingAdapter("recyclerData", "itemLayout", "itemViewType")
fun setData(
    recyclerView: ViewPager2,
    data: List<Any>?,
    @LayoutRes itemLayout: Int,
    itemViewTypeCreator: BaseBindingAdapter.ItemViewTypeCreator
) {
    setData(recyclerView, data, itemLayout, null, itemViewTypeCreator, null)
}

@BindingAdapter("recyclerData", "itemLayout", "itemClick")
fun setData(
    recyclerView: ViewPager2,
    data: List<Any>?,
    @LayoutRes itemLayout: Int,
    listener: BaseBindingAdapter.OnItemClickListener<Any>
) {
    setData(recyclerView, data, itemLayout, listener, null, null)
}

@BindingAdapter("recyclerData", "itemLayout", "itemClick", "itemViewType")
fun setData(
    recyclerView: ViewPager2,
    data: List<Any>?, @LayoutRes itemLayout: Int,
    listener: BaseBindingAdapter.OnItemClickListener<Any>,
    itemViewTypeCreator: BaseBindingAdapter.ItemViewTypeCreator
) {
    setData(recyclerView, data, itemLayout, listener, itemViewTypeCreator, null)
}

@BindingAdapter("recyclerData", "itemLayout", "itemClick", "itemEventHandler")
fun setData(
    recyclerView: ViewPager2,
    data: List<Any>?, @LayoutRes itemLayout: Int,
    listener: BaseBindingAdapter.OnItemClickListener<Any>,
    itemEventHandler: Any?
) {
    setData(recyclerView, data, itemLayout, listener, null, itemEventHandler)
}

@BindingAdapter("recyclerData", "itemLayout", "itemViewType", "itemEventHandler")
fun setData(
    recyclerView: ViewPager2,
    data: List<Any>?, @LayoutRes itemLayout: Int,
    itemViewTypeCreator: BaseBindingAdapter.ItemViewTypeCreator?,
    itemEventHandler: Any?
) {
    setData(recyclerView, data, itemLayout, null, itemViewTypeCreator, itemEventHandler)
}

@BindingAdapter("recyclerData", "itemLayout", "itemClick", "itemViewType", "itemEventHandler")
fun setData(
    recyclerView: ViewPager2,
    data: List<Any>?,
    @LayoutRes itemLayout: Int,
    listener: BaseBindingAdapter.OnItemClickListener<Any>?,
    itemViewTypeCreator: BaseBindingAdapter.ItemViewTypeCreator?,
    itemEventHandler: Any?
) {
    val adapter = recyclerView.adapter

    if (adapter == null) {
        val simpleBindingAdapter = DefaultBindingAdapter(itemLayout)
        simpleBindingAdapter.data = data
        simpleBindingAdapter.itemClickListener = listener
        simpleBindingAdapter.itemViewTypeCreator = itemViewTypeCreator
        simpleBindingAdapter.itemEventHandler = itemEventHandler
        try {
            recyclerView.adapter = simpleBindingAdapter
        } catch (e: Exception) {
            e.printStackTrace()
        }
    } else if (adapter is BaseBindingAdapter<*, *>) {
        (adapter as BaseBindingAdapter<Any, ViewDataBinding>).data = data

        adapter.itemViewTypeCreator = itemViewTypeCreator
        adapter.itemClickListener = listener
        adapter.itemEventHandler = itemEventHandler
    }
}

/**
 * 禁止滚动
 *
 * @param scroll 禁止滚动
 */
@BindingAdapter("limitScroll")
fun setLimitScroll(view: ViewPager2, scroll: Boolean) {
    view.isNestedScrollingEnabled = scroll
}


/**
 * RecycleView设置竖直分割线
 *
 * @param dividerSize 竖直分割线
 */
@BindingAdapter("verticalSpace")
fun setVerticalSpace(recyclerView: ViewPager2, dividerSize: Float) {
    val itemVDecoration = DividerItemDecoration(recyclerView.context, LinearLayout.VERTICAL)
    val drawable = GradientDrawable()
    drawable.setSize(0, dividerSize.toInt())
    drawable.setColor(Color.TRANSPARENT)
    itemVDecoration.setDrawable(drawable)
    recyclerView.addItemDecoration(itemVDecoration)
}

/**
 * RecycleView设置横向分割线
 *
 * @param dividerSize 横向分割线
 */
@BindingAdapter("horizontalSpace")
fun setHorizontalSpace(recyclerView: ViewPager2, dividerSize: Float) {
    val itemVDecoration = DividerItemDecoration(recyclerView.context, LinearLayout.HORIZONTAL)
    val drawable = GradientDrawable()
    drawable.setSize(dividerSize.toInt(), 0)
    drawable.setColor(Color.TRANSPARENT)
    itemVDecoration.setDrawable(drawable)
    recyclerView.addItemDecoration(itemVDecoration)
}
